//判定是否奇数
fn is_odd(n: u32) -> bool {
    n %2 == 1
}

fn main(){
    println!("Find the sum of all the squared odd numbers under 1000");
    let max = 1000;
    //使用普通命令（imperative）方式实现
    
    //声明累加器变量
    let mut acc = 0;
    //迭代0, 1, 2, ..至无穷大
    for n in 0u32.. {
        let n_squared = n.pow(2);
        //let n_squared = n * n;
        if n_squared >= max {
            break;
        } else if is_odd(n_squared) {
            acc += n_squared;
    }
}
println!("imperative style: {acc}");
//函数式的实现
let sum_of_squared_odd_numbers: u32 = 
    (0..).map(|n:u32|n.pow(2))             //所有自然数取平方
    //(0..).map(|n|n * n)             //所有自然数取平方
         .take_while(|&n| n < max)  //取小于上限的
         .filter(|&n| n % 2 == 1)   // 取奇数
         .fold(0, |sum, i| sum + i); //最后加起来
println!("functional style: {sum_of_squared_odd_numbers}");
}
